package com.ruoyi.yipaiju.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.yipaiju.domain.*;
import com.ruoyi.yipaiju.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/marketer")
public class MarketerController extends BaseController {

    @Autowired
    private IHouseUploadingOrderService houseUploadingOrderService;
    @Autowired
    private IForeclosedHouseBookingorderService foreclosedHouseBookingorderService;
    @Autowired
    private IForeclosedHouseSigningorderService foreclosedHouseSigningorderService;
    @Autowired
    private IDiscountHouseBookingorderService discountHouseBookingorderService;
    @Autowired
    private IDiscountHouseSigningorderService discountHouseSigningorderService;

    /******************放盘*************************/
    /**
     * 放盘签约订单修改
     * @param id
     * @return
     */
    @PostMapping("/houses/uploading/orders/{id}")
    public ApiResult houseuploadingOrdersInfo(@PathVariable("id") Long id, @RequestBody HouseUploadingOrder houseUploadingOrder) {
        HouseUploadingOrder houseUploadingOrder1 = houseUploadingOrderService.selectHouseUploadingOrderById(id);
        houseUploadingOrder1.setSubmitStatus(houseUploadingOrder.getSubmitStatus());
        if (houseUploadingOrder1.getSubmitStatus() != null && houseUploadingOrder1.getSubmitStatus().intValue() == 1){
            // 专员重新修改了 验证状态改为待审核 也就是提交
            houseUploadingOrder1.setMarketManagerVertifyStatus(1);
        }
        houseUploadingOrderService.updateHouseUploadingOrder(houseUploadingOrder1);

        // TODO 这里应该是修改订单状态   不知道怎么改。。。  先放这
//        houseUploadingOrder.setoStage();
//        houseUploadingOrder.setCancelStage();
        return ApiResult.success(houseUploadingOrder);
    }


    /**************优选***************************/
    /**
     * 优选预约订单修改
     * @param id
     * @param foreclosedHouseBookingorder
     * @return
     */
    @PostMapping("/houses/foreclosed/booking/orders/{id}")
    public ApiResult housesForeclosedBookingOrdersInfo(@PathVariable("id") Long id, @RequestBody ForeclosedHouseBookingorder foreclosedHouseBookingorder) {
        foreclosedHouseBookingorder.setId(id);
        foreclosedHouseBookingorderService.updateForeclosedHouseBookingorder(foreclosedHouseBookingorder);
        ForeclosedHouseBookingorder foreclosedHouseBookingorder1 = foreclosedHouseBookingorderService.selectForeclosedHouseBookingorderById(id);
        return ApiResult.success(foreclosedHouseBookingorder1);
    }

    /**
     * 优选签约订单修改
     * @param id
     * @param foreclosedHouseSigningorder
     * @return
     */
    @PostMapping("/houses/foreclosed/signing/orders/{id}")
    public ApiResult housesForeclosedSigningOrdersInfo(@PathVariable("id") Long id, @RequestBody ForeclosedHouseSigningorder foreclosedHouseSigningorder) {
        foreclosedHouseSigningorder.setId(id);
        if (foreclosedHouseSigningorder.getSubmitStatus() != null && foreclosedHouseSigningorder.getSubmitStatus().intValue() == 1){
            // 专员重新修改了 验证状态改为待审核 也就是提交
            foreclosedHouseSigningorder.setFinancialManagerVertifyStatus(1);
            foreclosedHouseSigningorder.setMarketManagerVertifyStatus(1);
            foreclosedHouseSigningorder.setSubmitTime(new Date());
        }
        if (foreclosedHouseSigningorder.getStage() != null){
            if (foreclosedHouseSigningorder.getStage().intValue() == 5){
                foreclosedHouseSigningorder.setAuctionDealTime(new Date());
            }

            if (foreclosedHouseSigningorder.getStage().intValue() == 8){
                foreclosedHouseSigningorder.setServiceFinishTime(new Date());
                foreclosedHouseSigningorder.setoStage(3);  // 待评价
            }

            if (foreclosedHouseSigningorder.getStage().intValue() == 9){
                foreclosedHouseSigningorder.setoStage(2);
                foreclosedHouseSigningorder.setDefeatTime(new Date());
            }
        }

        if (foreclosedHouseSigningorder.getRefundMethod() != null){
            foreclosedHouseSigningorder.setRefundSubmitTime(new Date());
        }

        foreclosedHouseSigningorderService.updateForeclosedHouseSigningorder(foreclosedHouseSigningorder);
        ForeclosedHouseSigningorder foreclosedHouseSigningorder1 = foreclosedHouseSigningorderService.selectForeclosedHouseSigningorderById(id);
        return ApiResult.success(foreclosedHouseSigningorder1);
    }


    /****************折扣************************/
    /**
     * 折扣预约订单修改
     * @param id
     * @param discountHouseBookingorder
     * @return
     */
    @PostMapping("/houses/discount/booking/orders/{id}")
    public ApiResult housesDiscountBookingOrdersInfo(@PathVariable("id") Long id, @RequestBody DiscountHouseBookingorder discountHouseBookingorder) {
        discountHouseBookingorder.setId(id);
        discountHouseBookingorderService.updateDiscountHouseBookingorder(discountHouseBookingorder);
        DiscountHouseBookingorder discountHouseBookingorder1 = discountHouseBookingorderService.selectDiscountHouseBookingorderById(id);
        return ApiResult.success(discountHouseBookingorder1);
    }

    /**
     * 折扣约订单修改
     * @param id
     * @param discountHouseSigningorder
     * @return
     */
    @PostMapping("/houses/discount/signing/orders/{id}")
    public ApiResult housesDiscountSigningOrdersInfo(@PathVariable("id") Long id, @RequestBody DiscountHouseSigningorder discountHouseSigningorder) {
        discountHouseSigningorder.setId(id);
        if (discountHouseSigningorder.getSubmitStatus() != null && discountHouseSigningorder.getSubmitStatus().intValue() == 1){
            // 专员重新修改了 验证状态改为待审核 也就是提交
            discountHouseSigningorder.setFinancialManagerVertifyStatus(1);
            discountHouseSigningorder.setMarketManagerVertifyStatus(1);
            discountHouseSigningorder.setSubmitTime(new Date());
        }
        if (discountHouseSigningorder.getStage() != null){
            if (discountHouseSigningorder.getStage().intValue() == 5){
                discountHouseSigningorder.setAuctionDealTime(new Date());
            }

            if (discountHouseSigningorder.getStage().intValue() == 8){
                discountHouseSigningorder.setServiceFinishTime(new Date());
                discountHouseSigningorder.setoStage(3);  // 待评价
            }

            if (discountHouseSigningorder.getStage().intValue() == 9){
                discountHouseSigningorder.setoStage(2);
                discountHouseSigningorder.setDefeatTime(new Date());
            }
        }

        if (discountHouseSigningorder.getRefundMethod() != null){
            discountHouseSigningorder.setRefundSubmitTime(new Date());
        }

        discountHouseSigningorderService.updateDiscountHouseSigningorder(discountHouseSigningorder);
        DiscountHouseSigningorder discountHouseSigningorder1 = discountHouseSigningorderService.selectDiscountHouseSigningorderById(id);
        return ApiResult.success(discountHouseSigningorder1);
    }


}
